<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SQLTable
 *
 * @author vlad
 */
class SQLTable extends SQLStruct {
    
    private $_columnNames;
    private $_columns;
            
    public function __construct($dbh, $tableName) {
        $this->setDBHandle($dbh);
        if (!empty($tableName)) {
            $this->_name = $tableName;
            $this->collectInformation();
        }
        return $this;
    }   
    
    private function collectInformation() {
        // print "\tCollecting information on table ".$this->_name."\n";
        $query = "show full columns from `".$this->_name."`;";
        $res = $this->_dbh->Q($query);
        while ($row = mysql_fetch_assoc($res)) {
            $this->_columnNames[] = $row['Field'];            
            $this->_columns[] = new SQLColumn(&$this->_dbh, $row);
        }
    }
    
    public function GetColumns() {
        return $this->_columns;
    }
    
    public function GetPrimaryKeyName() {
        foreach ($this->_columns as $column) {
            if ($column->IsPrimaryKey()) {
                return $column."";
            }
        }
        return "";
    }

    public function __toString() {
        return $this->_name;
    }
}

?>
